﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using VNet.VisiSuite.VisiAdmin.Common;
using VNet.VisiSuite.Common.Helper;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.Common;

namespace VNet.VisiSuite.VisiAdmin.Models
{
    public class UserModel : ComparableBaseModel<UserModel>, IDataErrorInfo
    {
        #region Fields
       
        private string _firstName;
        private string _lastName;
        private string _email;
        private string _login;
        private string _password;
        private string _passwordConfirmed;

        private bool _isEnabled;
        private bool _isEditPart;
        private bool _isEditUsers;
        
        
        private string _lastUpdatedByUser;
        private string _lastUpdatedDate;
        
        private int _userLoginId;
        #endregion

        #region Properties

        public int UserLoginId
        {
            get { return _userLoginId; }
            set
            {
                if (_userLoginId != value)
                {
                    _userLoginId = value;
                    OnPropertyChanged("UserLoginId");
                }
            }
        }

        [Category("General")]
        [DisplayName("First Name")]
        [Description("User first name.")]
        [DefaultValue("")]
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName", true);
            }
        }

        [Category("General")]
        [DisplayName("Last Name")]
        [Description("User last name.")]
        [DefaultValue("")]
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName", true);
            }
        }

        [Category("General")]
        [DisplayName("Email")]
        [Description("User email.")]
        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                OnPropertyChanged("Email", true);
            }
        }

        [Category("General")]
        [DisplayName("Login")]
        [Description("User login.")]
        public string Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged("Login");
            }
        }

        [Category("General")]
        [DisplayName("Password")]
        [Description("User password.")]
        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged("Password", true);
            }
        }

        [Category("General")]
        [DisplayName("Password Confirmed")]
        [Description("User password.")]
        public string PasswordConfirmed
        {
            get { return _passwordConfirmed; }
            set
            {
                _passwordConfirmed = value;
                OnPropertyChanged("PasswordConfirmed", true);
            }
        }


        [Category("Permissions")]
        [DisplayName("Enabled")]
        [Description("Enable / disable this user.")]
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled", true);
            }
        }

        [Category("Permissions")]
        [DisplayName("Can Edit Parts")]
        [Description("User can/cannot edit parts.")]
        public bool IsEditPart
        {
            get { return _isEditPart; }
            set
            {
                _isEditPart = value;
                OnPropertyChanged("IsEditPart", true);
            }
        }

        [Category("Permissions")]
        [DisplayName("Can Edit Users")]
        [Description("User can / cannot edit users")]
        public bool IsEditUsers
        {
            get { return _isEditUsers; }
            set
            {
                _isEditUsers = value;
                OnPropertyChanged("IsEditUsers", true);
            }
        }


        [Category("Update History")]
        [DisplayName("Created By")]
        [Description("User who created the user in VisiAdmin.")]
        public string CreatedByUser { get; set; }

        [Category("Update History")]
        [DisplayName("Created On")]
        [Description("Date when the user was created in VisiAdmin.")]
        public string CreatedDate { get; set; }

        [Category("Update History")]
        [DisplayName("Last Updated By")]
        [Description("User who last edited the user in VisiAdmin.")]
        public string LastUpdatedByUser
        {
            get { return _lastUpdatedByUser; }
            set
            {
                _lastUpdatedByUser = value;
                OnPropertyChanged("LastUpdatedByUser");
            }
        }

        [Category("Update History")]
        [DisplayName("Last Updated Date")]
        [Description("Date when the user was last edited in VisiAdmin.")]
        public string LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set
            {
                _lastUpdatedDate = value;
                OnPropertyChanged("LastUpdatedDate");
            }
        }

        public string Enabled
        {
            get { return IsEnabled == false ? "Disabled" : "Enabled"; }
        }

        #endregion

        #region IDataErrorInfo Implementation

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                if (!ShouldValidate)
                    return string.Empty;

                if (columnName == "FirstName" && string.IsNullOrWhiteSpace(FirstName))
                    return ValidationMessages.FirstNameRequired;
                if (columnName == "LastName" && string.IsNullOrWhiteSpace(LastName))
                    return ValidationMessages.LastNameRequired;
                if (columnName == "Login" && string.IsNullOrWhiteSpace(Login))
                    return ValidationMessages.LoginRequired;
                if (columnName == "Email" && !string.IsNullOrWhiteSpace(Email) && !CommonConstants.EmailRegex.Match(Email.TrimSafety()).Success)
                    return ValidationMessages.EmailInvalidFormat;
                if (columnName == "Password")
                {
                    if (IsAddNew && string.IsNullOrWhiteSpace(Password))
                        return ValidationMessages.PasswordRequired;
                    if (!string.IsNullOrWhiteSpace(PasswordConfirmed))
                        if (string.IsNullOrWhiteSpace(Password))
                            return ValidationMessages.PasswordRequired;
                        else if (Password.Length < 6)
                            return ValidationMessages.PasswordMustbeGreaterSixCharacters;
                }
                else if (columnName == "PasswordConfirmed" && !string.IsNullOrWhiteSpace(Password))
                {
                    if (!string.IsNullOrWhiteSpace(Password))
                        if (string.IsNullOrWhiteSpace(PasswordConfirmed))
                            return ValidationMessages.PasswordConfirmedRequired;
                        else if (PasswordConfirmed != Password)
                            return ValidationMessages.PasswordConfirmedNotEqualsPassword;
                }
                return string.Empty; ;
            }
        }

        #endregion

        #region Override Equals methods
        public bool Equals(UserModel other)
        {
            if (ReferenceEquals(null, other))
                return false;
            if (ReferenceEquals(this, other))
                return true;

            return string.Equals(FirstName, other.FirstName)
                && string.Equals(LastName, other.LastName)
                && string.Equals(Email, other.Email)
                && string.Equals(Password, other.Password)
                && IsEnabled == other.IsEnabled
                && IsEditPart == other.IsEditPart
                && IsEditUsers == other.IsEditUsers
                && string.Equals(PasswordConfirmed, other.PasswordConfirmed);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != this.GetType())
                return false;
            return Equals((UserModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var result = 0;
                result = (result * 397) ^ (FirstName ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (LastName ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Email ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (Password ?? string.Empty).GetHashCode();
                result = (result * 397) ^ (PasswordConfirmed ?? string.Empty).GetHashCode();
                result = (result * 397) ^ IsEnabled.GetHashCode();
                result = (result * 397) ^ IsEditPart.GetHashCode();
                result = (result * 397) ^ IsEditUsers.GetHashCode();
                return result;
            }
        }
        #endregion
    }
}
